﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using RoadTeam_Generate.SQL;
using System.IO;
using RoadTeam_Generate;


namespace Evlution_Generate
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private bool CheckControl()
        {
            return true;

            if (String.IsNullOrEmpty(textBox3.Text))
            {
                MessageBox.Show("筛选条件不能为空");
                return false;
            }

            if (String.IsNullOrEmpty(textBox2.Text))
            {
                MessageBox.Show("实体类存放位置不能为空");
                return false;
            }

            if (String.IsNullOrEmpty(this.comboBox1.Text))
            {
                MessageBox.Show("连接字符串不能为空");
                return false;
            }

            return true;
        }
        //填充选择的实体类存放路径
        private void button1_Click(object sender, EventArgs e)
        {
            FolderBrowserDialog dialog = new FolderBrowserDialog();
            dialog.Description = "实体文件存放位置";
            if (dialog.ShowDialog() == DialogResult.OK)
            {
                this.textBox2.Text = dialog.SelectedPath;
            }
        }

        private void button2_Click(object sender, EventArgs e)
        {

            if (!CheckControl()) return;


            #region 创建sysproperties系统视图
            SqlHelp.CreateInstance(this.comboBox1.Text).ExcuteQuery(SqlEmun.existssysproperties, CommandType.Text);
            SqlHelp.CreateInstance(this.comboBox1.Text).ExcuteQuery(SqlEmun.Createsysproperties, CommandType.Text);
            #endregion

            #region 创建数据库视图
            SqlHelp.CreateInstance(this.comboBox1.Text).ExcuteQuery(SqlEmun.existDBSYSINFO, CommandType.Text);
            SqlHelp.CreateInstance(this.comboBox1.Text).ExcuteQuery(SqlEmun.CreateDBSYSINFO, CommandType.Text);
            #endregion

            String StrSql = @"select name as [表名称],crdate as [创建时间] from sysobjects where xtype='U' AND name LIKE'%" + textBox3.Text + "%' ";
            dataGridView1.DataSource = SqlHelp.CreateInstance(this.comboBox1.Text).ExcuteQuery(StrSql, CommandType.Text);

            dataGridView1.Columns[0].Width = 350;
            dataGridView1.Columns[1].Width = 243;
            this.dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void 生成实体类ToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (!CheckControl()) return;

            List<String> LiTableName = new List<String>();
            for (int i = 0; i < dataGridView1.SelectedRows.Count; i++)
            {
                DataRow dr = (dataGridView1.SelectedRows[i].DataBoundItem as DataRowView).Row;
                LiTableName.Add(dr["表名称"].ToString());
            }

            DataTable dt;
            String Str_Temp = String.Empty;
            progressBar1.Maximum = LiTableName.Count;
            progressBar1.Minimum = 0;
            progressBar1.Value = 0;
            foreach (String TableName in LiTableName)
            {
                Str_Temp = @"select * from DBSYSINFO where [表名]='" + TableName + "'";
                dt = SqlHelp.CreateInstance(this.comboBox1.Text).ExcuteQuery(Str_Temp);


                using (TextWriter writer = new StreamWriter(
                     new BufferedStream(
                     new FileStream(this.textBox2.Text + "\\" + TableName + ".cs", FileMode.Create, FileAccess.Write)),
                     System.Text.Encoding.GetEncoding("gb2312")))
                {
                    writer.WriteLine(CreateSqlModel.GetGenerateCode(TableName, dt));
                }

                progressBar1.Value++;
            }
            if (LiTableName.Count == 0)
                MessageBox.Show("没有选择任何表");
            else
                MessageBox.Show("实体类生成成功");
        }

        private void 退出ToolStripMenuItem2_Click(object sender, EventArgs e)
        {
            Application.Exit();
        }

        private void menuStrip1_ItemClicked(object sender, ToolStripItemClickedEventArgs e)
        {

        }

        private void Form1_Load(object sender, EventArgs e)
        {
            //接口服务器连接字符串
            //textBox1.Text = @"Data Source=SHARKLOCK-PC;Initial Catalog=RoadTeamWebService;User ID=sa;Password=RoadTeam1230";
            //textBox2.Text = @"E:\陈柯良\RoadTeam_WebService\RoadTeam.Model"; 
            //textBox2.Text = @"F:\RoadTeam_WebService\RoadTeam\RoadTeam.Model\SysModel";
            //textBox2.Text = @"D:\RoadTeam_WebSerProject\RoadTeam.Model\SysModel";
            //对接CS系统
            //textBox1.Text = @"Data Source=SHARKLOCK-PC;Initial Catalog=WY_测试专用II;User ID=sa;Password=RoadTeam1230";
            //textBox2.Text = @"E:\陈柯良\RoadTeam_WebService\RoadTeam.Model\Model_CS"; 
            //对接BS系统
            //textBox1.Text = @"Data Source=SHARKLOCK-PC;Initial Catalog=ZL;User ID=sa;Password=RoadTeam1230";
            //textBox1.Text = @"Data Source=SHARKLOCK-PC;Initial Catalog=仲量;User ID=sa;Password=RoadTeam1230";
            //textBox2.Text = @"E:\陈柯良\RoadTeam_WebService\RoadTeam.Model\Model_BS";
            //textBox2.Text = @"D:\RoadTeam_WebSerProject\RoadTeam.Model\Model_BS";
            //textBox2.Text = @"F:\RoadTeam_WebService\RoadTeam\RoadTeam.Model\Model_BS"; 
            this.comboBox1.Items.Add(@"Data Source=SHARKLOCK-PC;Initial Catalog=RoadTeamWebService;User ID=sa;Password=RoadTeam1230");
            this.comboBox1.Items.Add(@"Data Source=SHARKLOCK-PC;Initial Catalog=ZL;User ID=sa;Password=RoadTeam1230");
        }


        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
              // textBox2.Text = @"E:\陈柯良\RoadTeam_WebService\RoadTeam.Model";
            textBox2.Text = @"D:\RoadTeam_WebSerProject\RoadTeam.Model";

        }
    }
}
